package com.springboot.common.entity.response;

import java.io.Serializable;
import lombok.Data;

/**
 * 结果类
 *
 * @author supu
 * @date 2019-03-19 13:49
 **/
@Data
public class Result<T extends Object> implements Serializable {
    private static final long serialVersionUID = 2092655786768692492L;

    private static final int SUCCESS = 200;
    private static final int FAILURE = -100;

    /**
     * 状态码
     */
    private int code;
    /**
     * 消息内容
     */
    private String msg;
    /**
     * 返回结果数据
     */
    private T data;
    /**
     * 结果boolean值
     */
    private boolean isSuccess;

    public Result() {

    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.isSuccess = (code == SUCCESS);
    }

    public static <T> Result<T> newSuccess() {
        return new Result<>(SUCCESS, "success", null);
    }

    public static <T> Result<T> newSuccess(T data) {
        return new Result<>(SUCCESS, "success", data);
    }

    public static <T> Result<T> newFailure(int code, String errorMsg) {
        return new Result<>(code, errorMsg, null);
    }

    public static <T> Result<T> newFailure(String errorMsg) {
        return newFailure(FAILURE, errorMsg);
    }
}
